﻿#include "taskmanager.h"
double instructionIntervalTime=0.1;
double stableTime=0.2;
double FulldiskstableTime=0.2;
double ChinaScanstableTime=0.1;
double MidReginstableTime=0.1;
double LoopTime=25;
double pretime=0.3;
double afttime=0.1;
int FulldiskpreSpace=0;
int ChinascanpreSpace=0;
double fulldispreangle=0.0;
TaskManager* TaskManager::mInstance = NULL;
TaskManager::TaskManager(QObject *parent) : QObject(parent)
  ,mpInstrument(NULL)
{
    this->setObjectName("ObjectTaskManager");
    mInsType = "FY4C-Agr";

    mvctTasks.clear();
    mvctStyles.clear();
    mvctNotify.clear();
    mvctregion.clear();
    allregionInfos.clear();
    CurEidtMidNum=0;

#if 0 // 临时，添加一些样式
    InstrumentBase* pCurIns = this->currentInstrument();
    QStringList lstTypes = pCurIns->taskTypes();

    int red = 0;
    int green = 100;
    int blue = 0;
    foreach (QString s, lstTypes) {
        StyleItem* pStyle = this->addStyle(s);

        QRgb rbg = qRgb(red,green,blue);
        pStyle->setBrushColor(rbg);

        blue += 255/lstTypes.count();
    }

#endif
    QDir settingDir(QDir::currentPath());
    settingDir.cdUp();
    settingDir.setPath(settingDir.absolutePath().append("/config/FY4TaskOptimization"));

    if(!settingDir.exists())
    {
        settingDir.mkdir(settingDir.absolutePath());
    }

    QSettings settings(settingDir.absoluteFilePath("setting.ini"),QSettings::IniFormat);
    settings.beginGroup("InstrumentBase");
    QString sIns = settings.value("instrument").toString();
    if(sIns.isEmpty())
    {
        sIns = QString("FY4C-Agr");
        settings.setValue("instrument",sIns);
    }

    if(sIns.contains("FY4C-Agr",Qt::CaseInsensitive))
    {
        mpInstrument = new F4CAgrInstrument();
    }

    if(sIns.contains("FY4B-Agr",Qt::CaseInsensitive))
    {
        mpInstrument = new F4BAgrInstrument();
        mInsType=sIns;
    }

    if(sIns.contains("FY4T-Agr",Qt::CaseInsensitive))
    {
        mpInstrument = new f4testimatrument();
        mInsType=sIns;
    }

    settings.endGroup();

    settings.beginGroup(mInsType.append("-InstructionTime"));
    if(settings.contains("FulldiskpreSpace"))
    {
        FulldiskpreSpace=settings.value("FulldiskpreSpace").toInt();
    }
    else
        settings.setValue("FulldiskpreSpace",FulldiskpreSpace);

    if(settings.contains("ChinascanpreSpace"))
    {
        ChinascanpreSpace=settings.value("ChinascanpreSpace").toInt();
    }
    else
        settings.setValue("ChinascanpreSpace",ChinascanpreSpace);

    if(settings.contains("instructionIntervalTime"))
    {
        instructionIntervalTime=settings.value("instructionIntervalTime").toDouble();
    }
    else
        settings.setValue("instructionIntervalTime",QString::number(instructionIntervalTime,'f',6));

    if(settings.contains("stableTime"))
    {
        stableTime=settings.value("stableTime").toDouble();
    }
    else
        settings.setValue("stableTime",QString::number(stableTime,'f',6));

    if(settings.contains("FulldiskstableTime"))
    {
        FulldiskstableTime=settings.value("FulldiskstableTime").toDouble();
    }
    else
        settings.setValue("FulldiskstableTime",QString::number(FulldiskstableTime,'f',6));

    if(settings.contains("fulldispreangle"))
    {
        fulldispreangle=settings.value("fulldispreangle").toDouble();
    }
    else
        settings.setValue("fulldispreangle",QString::number(fulldispreangle,'f',6));

    if(settings.contains("ChinaScanstableTime"))
    {
        ChinaScanstableTime=settings.value("ChinaScanstableTime").toDouble();
    }
    else
        settings.setValue("ChinaScanstableTime",QString::number(ChinaScanstableTime,'f',6));

    if(settings.contains("MidReginstableTime"))
    {
        MidReginstableTime=settings.value("MidReginstableTime").toDouble();
    }
    else
        settings.setValue("MidReginstableTime",QString::number(MidReginstableTime,'f',6));

    if(settings.contains("LoopTime"))
    {
        LoopTime=settings.value("LoopTime").toDouble();
    }
    else
        settings.setValue("LoopTime",LoopTime);
    if(settings.contains("pretime"))
        pretime=settings.value("pretime").toDouble();
    else
    {
        settings.setValue("pretime",QString::number(pretime,'f',6));
    }
    if(settings.contains("afttime"))
        pretime=settings.value("afttime").toDouble();
    else
    {
        settings.setValue("afttime",QString::number(pretime,'f',6));
    }
    settings.endGroup();
    // 通过配置文件读取样式配置信息
    loadStyleFile();

}

TaskManager *TaskManager::getInstance()
{
    if(mInstance == NULL)
    {
        mInstance = new TaskManager();
    }

    return mInstance;
}

QString TaskManager::curIns()
{
    return currentInstrument()->instrumentName();
}

void TaskManager::setcurMidNum(int num)
{
    CurEidtMidNum=num;
}

int TaskManager::getcurMidNum()
{
    return CurEidtMidNum;
}

void TaskManager::removecurMidNum(int num)
{
    allregionInfos.removeAt(num-1);
}

QVector<TaskItem *> TaskManager::allTasks()
{
    return mvctTasks;
}

TaskItem *TaskManager::addTask(const QString &sName)
{
    TaskItem* pItem = new TaskItem(sName);

    mvctTasks.append(pItem);

    return pItem;
}

bool TaskManager::removeTask(const QString &objName)
{
    foreach (TaskItem* pTask, mvctTasks) {
        if(pTask->objectName()==objName)
        {
            for(int i=0;i<mvctNotify.count();++i)
            {
                mvctNotify.at(i)->removeTask(objName);
            }
            mvctTasks.removeOne(pTask);

            delete pTask;
            pTask = NULL;

            return true;
        }
    }
    return false;
}
TaskItem *TaskManager::getTask(const QString &objName)
{
    foreach (TaskItem* pTask, mvctTasks) {
        if(pTask->objectName() == objName)
        {
            return pTask;
        }
    }
    return NULL;
}

QVector<CommandItem *> TaskManager::getSortCommand()
{
    QVector<CommandItem* > vctCommand;
    vctCommand.clear();

    foreach (TaskItem* pTask, mvctTasks) {
        vctCommand.append(pTask->allCommands());
    }

    for(int i=0; i<vctCommand.count()-1; ++i)
    {
        for(int j=i+1; j<vctCommand.count(); ++j)
        {
            if(vctCommand.at(j)->getValue(ScriptItem::Time_Start) < vctCommand.at(i)->getValue(ScriptItem::Time_Start))
            {
                vctCommand.move(j,i);
            }
            else
            {
                continue;
            }
        }
    }

    return vctCommand;
}

void TaskManager::loadStyleFile()
{
    QDir settingDir(QDir::currentPath());
    settingDir.cdUp();
    settingDir.setPath(settingDir.absolutePath().append("/config/FY4TaskOptimization"));
    if(!settingDir.exists())
    {
        settingDir.mkdir(settingDir.absolutePath());
    }

    QSettings settings(settingDir.absoluteFilePath("setting.ini"),QSettings::IniFormat);
    settings.beginGroup("TaskStyle");

    // 系统默认赋值
    if(!settings.contains("FastPointing"))
    {
        settings.setValue(QString("FastPointing"),qRgb(100,100,100));
    }

    StyleItem* pStyle = this->getStyle("FastPointing");
    if(NULL == pStyle)
    {
        pStyle = this->addStyle("FastPointing");
        pStyle->setBrushColor(QRgb(settings.value("FastPointing").toInt()));
    }

    InstrumentBase * curIns = this->currentInstrument();
    if(NULL == curIns)
    {
        settings.endGroup();
        return;
    }

    //1009对于任务颜色的修改应该在这个位置!!
    int red = 0;
    int green = 100;
    int blue = 0;
    QStringList lstTask = curIns->taskTypes();
    foreach (QString taskType, lstTask) {
        QRgb rbg = qRgb(red,green,blue);
        if(settings.contains(taskType))
        {
            rbg = settings.value(taskType).toInt();
        }
        else
        {
            settings.setValue(taskType,rbg);
        }
        StyleItem* pStyle = this->getStyle(taskType);
        if(NULL == pStyle)
        {
            pStyle = this->addStyle(taskType);
            pStyle->setBrushColor(rbg);
        }
        blue += 255/lstTask.count();
    }
    settings.endGroup();
}

StyleItem *TaskManager::addStyle(QString sName)
{
    StyleItem* pStyle = getStyle(sName);
    if(NULL == pStyle)
    {
        pStyle = new StyleItem(sName,this);
    }
    mvctStyles.append(pStyle);

    return pStyle;
}

StyleItem *TaskManager::getStyle(const QString &sName)
{
    foreach (StyleItem* pStyle, mvctStyles) {
        if(pStyle->getType() == sName)
        {
            return pStyle;
        }
    }

    return NULL;
}

QVector<StyleItem *> TaskManager::allStyles()
{
    return mvctStyles;
}



bool TaskManager::removeStyle(const QString &sName)
{
    foreach (StyleItem* pStyle, mvctStyles) {
        if(pStyle->getType() == sName)
        {
            mvctStyles.removeOne(pStyle);
            delete pStyle;
            return true;
        }
    }

    return false;
}

void TaskManager::updateStyle(const QString &sName)
{
    foreach (Notify* pNotify, mvctNotify) {
        pNotify->updateStyle(sName);
    }
}

InstrumentBase *TaskManager::currentInstrument()
{
    //该处和构造函数是不是重复，可以去掉？？？
    //    if(NULL == mpInstrument)
    //    {
    //        QDir settingDir(QDir::currentPath());
    //        settingDir.cdUp();
    //        settingDir.setPath(settingDir.absolutePath().append("/config/FY4TaskOptimization"));
    //        if(!settingDir.exists())
    //        {
    //            settingDir.mkdir(settingDir.absolutePath());
    //        }

    //        QSettings settings(settingDir.absoluteFilePath("setting.ini"),QSettings::IniFormat);
    //        settings.beginGroup("InstrumentBase");
    //        QString sIns = settings.value("instrument").toString();
    //        if(sIns.isEmpty())
    //        {
    //            sIns = QString("FY4C-Agr");
    //            settings.setValue("instrument",sIns);
    //        }

    //        if(sIns.contains("FY4C-Agr",Qt::CaseInsensitive))
    //        {
    //            mpInstrument = new F4CAgrInstrument();
    //        }
    //        settings.endGroup();
    //    }
    return mpInstrument;
}

void TaskManager::registNotify(Notify *p)
{
    if(NULL == p)
        return;

    if(mvctNotify.contains(p))
        return;

    mvctNotify.append(p);
}

void TaskManager::slot_updateTask(QString taskObj, QString cmdObj, QString scriptObj)
{
    for(int i=0;i<mvctNotify.count();++i)
    {
        mvctNotify.at(i)->updateTask(taskObj,cmdObj,scriptObj);
    }
}

void TaskManager::slot_commandSelect(const QString &cmdObj)
{
    QObject* pSend = sender();
    Notify* pSendNotify = dynamic_cast<Notify* >(pSend);
    foreach (Notify* pNotify, mvctNotify) {
        if(pSendNotify == pNotify)
        {
            continue;
        }
        else
        {
            pNotify->commandSelect(cmdObj);
        }
    }
}

